/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 * File: debug.h
 *************************************************************************
 *\author: starlove
 *************************************************************************/

#ifndef __MLSE_LIBRARY_DEBUG_H__
#define __MLSE_LIBRARY_DEBUG_H__

#include <platform/platform.h>
#include <stdio.h>
#include <stdarg.h>

#define __MLSE_DEBUG_INFO_LIMIT__ 255


inline void DEBUG_INFO(const char *szFlag, int nFd, const char *f, ...)
{
	char szTmp[__MLSE_DEBUG_INFO_LIMIT__];
	va_list vl;
	va_start(vl, f);
#ifdef __MLSE_PLATFORM_WIN__
	vsnprintf_s(szTmp, __MLSE_DEBUG_INFO_LIMIT__, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
#else
	vsnprintf(szTmp, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
#endif
	va_end(vl);
	if(nFd > 0)
	{
		printf("\n[%s] (%d) %s\n", szFlag, nFd, szTmp);
	}else
		printf("\n[%s] %s\n", szFlag, szTmp);
}

#ifdef __MLSE_DEBUG_KERNEL_NETWORK__
#define DEBUG_INFO_NETWORK(s, f, ...)\
	DEBUG_INFO("NETWORK", s, f, ##__VA_ARGS__)
#else
#define DEBUG_INFO_NETWORK(s, f, ...)
#endif

#ifdef __MLSE_DEBUG_KERNEL_CODEC__
#define DEBUG_INFO_CODEC(f, ...)\
	DEBUG_INFO("CODEC", 0, f, ##__VA_ARGS__)
#else
#define DEBUG_INFO_CODEC(f, ...)
#endif

#ifdef __MLSE_DEBUG_KERNEL_LIBRARY__
#define DEBUG_INFO_LIBRARY(f, ...)\
	DEBUG_INFO("LIBRARY", 0, f, ##__VA_ARGS__)
#else
#define DEBUG_INFO_LIBRARY(f, ...)
#endif

#ifdef __MLSE_DEBUG_KERNEL_SCRIPT__
#define DEBUG_INFO_SCRIPT(f, ...)\
	DEBUG_INFO("SCRIPT", 0, f, ##__VA_ARGS__)
#else
#define DEBUG_INFO_SCRIPT(f, ...)
#endif
//
//inline void DEBUG_INFO_NETWORK(SOCKET s, const char *f, ...)
//{
//#ifdef __MLSE_DEBUG_KERNEL_NETWORK__
//	char szTmp[__MLSE_DEBUG_INFO_LIMIT__];
//	va_list vl;
//	va_start(vl, f);
//	VSNPRINTF(szTmp, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
//	va_end(vl);
//	printf("\n[NETWORK][%d] %s\n", s, szTmp);
//#endif
//}
//
//inline void DEBUG_INFO_CODEC(const char *f, ...)
//{
//#ifdef __MLSE_DEBUG_KERNEL_CODEC__
//	char szTmp[__MLSE_DEBUG_INFO_LIMIT__];
//	va_list vl;
//	va_start(vl, f);
//	vsnprintf(szTmp, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
//	va_end(vl);
//	printf("\n[CODEC] %s\n", szTmp);
//#endif
//}
//
//inline void DEBUG_INFO_LIBRARY(const char *f, ...)
//{
//#ifdef __MLSE_DEBUG_KERNEL_LIBRARY__
//	char szTmp[__MLSE_DEBUG_INFO_LIMIT__];
//	va_list vl;
//	va_start(vl, f);
//	vsnprintf(szTmp, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
//	va_end(vl);
//	printf("\n[LIBRARY] %s\n", szTmp);
//#endif
//}
//
//inline void DEBUG_INFO_SCRIPT(const char *f, ...)
//{
//#ifdef __MLSE_DEBUG_KERNEL_SCRIPT__
//	char szTmp[__MLSE_DEBUG_INFO_LIMIT__];
//	va_list vl;
//	va_start(vl, f);
//	vsnprintf(szTmp, __MLSE_DEBUG_INFO_LIMIT__ - 1, f, vl);
//	va_end(vl);
//	printf("\n[SCRIPT] %s\n", szTmp);
//#endif
//}

#endif // __MLSE_LIBRARY_DEBUG_H__
